use std::fs::OpenOptions;
use std::io::{self, Seek, SeekFrom, Read};
use std::time::Duration;
use std::thread;

const FILE_PATH: &str = "temp";
const FILE_SIZE: u64 = 4096;

fn main() -> io::Result<()> {
    let mut file = OpenOptions::new()
        .read(true)
        .write(true)
        .create(true)
        .open(FILE_PATH)?;

    file.set_len(FILE_SIZE)?; // Ensure the file is at least FILE_SIZE bytes

    let len = file.seek(SeekFrom::End(0))?; // Get current file size

    loop {
        thread::sleep(Duration::from_secs(1));

        // Seek to offset and read the data
        file.seek(SeekFrom::Start(1024))?;
        let mut buffer = vec![0; (len - 1024) as usize];
        file.read_exact(&mut buffer)?;

        // Convert buffer to string and print it
        if let Ok(string) = String::from_utf8(buffer) {
            println!("{}", string);
        }
    }
}
